package com.zm.demo.core.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zoum
 * @create 2020/1/19 9:57
 */
public class LengthOfLongestSubstring {


    public int lengthOfLongestSubstring(String s) {

        if (s == null || s.length() == 0){
            return 0;
        }

        int maxLength = 0;
        int startIndex = 0;
        int length = s.length();

        Map<Character, Integer> map = new HashMap<>();
        String subStr = s;
        for (int i = 0; i < subStr.length(); i++) {
            Character c = subStr.charAt(i);
            if (map.get(c) != null){
                maxLength = Math.max(maxLength, i-map.get(c));
            }else {
                maxLength++;
            }
            map.put(c, i);

        }


        return maxLength;
    }
}
